package com.easyidea.its.web.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.easyidea.its.domain.model.surveillance.VehicleInfo;
import com.easyidea.its.domain.service.surveillance.VehicleInfoService;

public class ShowCutImageServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

//		String firstPicPath = req.getParameter("firstPicPath");
//		if (firstPicPath != null) {
//			Integer left = req.getParameter("left") == null ? 0 : Integer.parseInt(req.getParameter("left")) ;
//			Integer top = req.getParameter("top") == null ? 0 : Integer.parseInt(req.getParameter("top")) ;
//			Integer height = req.getParameter("height") == null ? 0 : Integer.parseInt(req.getParameter("height")) ;
//			Integer width = req.getParameter("width") == null ? 0 : Integer.parseInt(req.getParameter("width")) ;
//			FTPConfig config = FTPClientUtil.buildFtpConfig(firstPicPath, left, top, height, width);
//
//				try{
//				InputStream hFile = FTPClientUtil.downLoadFtpFile(config);
//
//				Image img;
//				ImageFilter cropFilter;
//				BufferedImage bi = ImageIO.read(hFile);
//				int srcWidth = bi.getWidth(); // 源图宽度
//				int srcHeight = bi.getHeight(); // 源图高度
//				if (srcWidth >= config.getDestWidth()
//						&& srcHeight >= config.getDestHeight()) {
//					Image image = bi.getScaledInstance(srcWidth, srcHeight,
//							Image.SCALE_DEFAULT);
//					// 即: CropImageFilter(int x,int y,int width,int height)
//					cropFilter = new CropImageFilter(config.getTopLeftX(),
//							config.getTopLeftY(), config.getDestWidth(), config
//									.getDestHeight());
//					img = Toolkit.getDefaultToolkit().createImage(
//							new FilteredImageSource(image.getSource(),
//									cropFilter));
//					BufferedImage tag = new BufferedImage(
//							config.getDestWidth(), config.getDestHeight(),
//							BufferedImage.TYPE_INT_RGB);
//					Graphics g = tag.getGraphics();
//					g.drawImage(img, 0, 0, null); // 绘制缩小后的图
//					g.dispose();
//					// 输出为文件
//					ImageIO.write(tag, "JPEG", res.getOutputStream());
//				}
//			}catch(Exception e) {
//				Log.error(e.getMessage());
//			}
//		}
		WebApplicationContext context = WebApplicationContextUtils
		.getWebApplicationContext(this.getServletContext());
		VehicleInfoService vehicleInfoService = (VehicleInfoService)context.getBean("vehicleInfoService");
		String plateNumber = req.getParameter("plateNumber");
		res.setContentType("image/*"); // 设置返回的文件类型
		OutputStream toClient = res.getOutputStream(); // 得到向客户端输出二进制数据的对象
		VehicleInfo vehicleInfo = vehicleInfoService.getVehicleInfo(plateNumber);
		toClient.write(vehicleInfo.getMemo()); // 输出数据
		toClient.close();
	
	}

}